package com.tsugun.mediaplayer.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.BaseActivity;
import com.tsugun.mediaplayer.view.adapter.PlayListItemAdapter;

/**
 * 播放列表会话框
 * Created by shize on 2017/5/22.
 */

public class PlayListDialog extends Dialog {

    private BaseActivity baseActivity;

    private PlayListDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        baseActivity = (BaseActivity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        if (window != null) {
            SettingPreferenceHelper.INSTANCE.setNightModeTheme(window.getDecorView(), baseActivity);
        }
    }

    /**
     * 初始化会话框
     */
    private void initWindow() {
        Window w = getWindow();
        if (w == null) {
            return;
        }
        w.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        lp.windowAnimations = R.style.PlayListAnimation;
        w.setAttributes(lp);
    }

    public static class Builder {

        private Context mContext;
        private PlayListItemAdapter mAdapter;
        private OnDialogClickListener mListener;
        private PlayListDialog mDialog;
        private RelativeLayout mRlDialogBg;
        private TextView count;
        private int mBgColor;

        public Builder(Context context, PlayListItemAdapter adapter) {
            mContext = context;
            mAdapter = adapter;
            mBgColor = -1;
        }

        /**
         * 注册点击事件
         *
         * @param listener 事件监听器
         * @return builder
         */
        public Builder setOnDialogClickListener(OnDialogClickListener listener) {
            mListener = listener;
            return this;
        }

        /**
         * 设置背景色
         *
         * @param color 颜色
         * @return builder
         */
        public Builder setBgColor(int color) {
            mBgColor = color;
            if (mDialog != null) {
                mRlDialogBg.setBackgroundColor(mBgColor);
            }
            return this;
        }

        /**
         * 创建会话框
         *
         * @return 会话框
         */
        public PlayListDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            mDialog = new PlayListDialog(mContext, R.style.PlayListDialog);
            View layout = inflater.inflate(R.layout.layout_play_list, null);
            mDialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            initList(layout);
            mDialog.setContentView(layout);
            return mDialog;
        }

        /**
         * 初始化列表
         *
         * @param layout 布局视图
         */
        private void initList(View layout) {
            mAdapter.setOnPlayListClickListener(new PlayListItemAdapter.OnPlayListClickListener() {
                @Override
                public void onItemClick(int position) {
                    mListener.onItemClick(position);
                }

                @Override
                public void onDeleteClick(int position) {
                    mListener.onDeleteClick(position);
                    count.setText("(" + mAdapter.getItemCount() + ")");
                }
            });
            RecyclerView list = (RecyclerView) layout.findViewById(R.id.id_play_list_recycler);
            if (mAdapter == null) {
                return;
            }
            list.setAdapter(mAdapter);
            list.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
            mAdapter.updateListItem();
            count = (TextView) layout.findViewById(R.id.id_play_list_count);
            count.setText("(" + mAdapter.getItemCount() + ")");
            ImageButton clearButton = (ImageButton) layout.findViewById(R.id.id_play_list_delete);
            clearButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    count.setText(mContext.getString(R.string.str_list_count));
                    mListener.onClearClick();
                }
            });
            if (mBgColor != -1) {
                mRlDialogBg = (RelativeLayout) layout.findViewById(R.id.id_dialog_bg);
                mRlDialogBg.setBackgroundColor(mBgColor);
            }
        }

        /**
         * 更新播放列表
         */
        public void notifyChanged(){
            mAdapter.updateListItem();
        }

        /**
         * 会话框事件监听器
         */
        public interface OnDialogClickListener {
            /**
             * 清除点击事件
             */
            void onClearClick();

            /**
             * item点击事件
             *
             * @param pos 位置
             */
            void onItemClick(int pos);

            /**
             * item 删除按钮点击事件
             *
             * @param pos 位置
             */
            void onDeleteClick(int pos);
        }
    }
}
